package org.soenea.doitall.domain.command;

import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.command.CommandError;
import org.dsrg.soenea.domain.command.CommandException;
import org.dsrg.soenea.domain.command.DomainCommand;
import org.dsrg.soenea.domain.helper.Helper;
import org.soenea.doitall.domain.command.exception.UserisAdminException;
import org.soenea.doitall.domain.model.user.User;
import org.soenea.doitall.domain.model.user.mapper.UserInputMapper;

public class LoginCommand extends DomainCommand {

	public LoginCommand(Helper helper) {
		super(helper);
	}

	@Override
	public void setUp() throws CommandException {
	}

	@Override
	public void process() throws CommandException {
		try {
			System.out.println("LOGIN COMMAND PROCESS");
			String userName = helper.getString("userName");
			String password = helper.getString("password");
			System.out.println("userName: " + userName + " password:" + password );
			User user = UserInputMapper.find(userName,password);
			helper.setRequestAttribute("user", user);
			helper.setSessionAttribute("UserID", user.getId());
			helper.setSessionAttribute("GroupID", user.getGroupID());
			
			if(user.isAdmin()){
				
				throw new UserisAdminException();
			}
			System.out.println("userID: " + helper.getSessionAttribute("UserID") + " groupID:" + helper.getSessionAttribute("GroupID") );
		} catch (MapperException e) {
			throw new CommandException(e);
		}
	}

	@Override
	public void tearDown() throws CommandError {
	}

}
